package com.maizhe.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maizhe.dao.RoleMapper;
import com.maizhe.dao.RoleResourceMapper;
import com.maizhe.dao.UserMapper;
import com.maizhe.entity.Role;
import com.maizhe.entity.RoleResource;
import com.maizhe.entity.User;
import com.maizhe.service.RoleService;
import com.maizhe.service.UserService;
import com.maizhe.util.JsonResult;
import com.maizhe.util.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RoleResourceMapper roleResourceMapper;

	public PageInfo<Role> findRoleList(Role role) {
		PageHelper.startPage(role.getPage(),role.getRows());
		List<Role>list=roleMapper.findAll(role);
		return new PageInfo<Role>(list);
	}

	public int saveRole(Role role) {
		int i=0;
		if("".equalsIgnoreCase(role.getId())||role.getId()==null){//新增
			role.setId(UUIDGenerator.getUUID());
			i= roleMapper.saveRole(role);
		}else{//编辑
			i= roleMapper.editRole(role);
		}
		return  i;
	}

	public Role findById(String id) {
		return roleMapper.findById(id);
	}

	public int delete(String id) {
		return roleMapper.remove(id);
	}

	public List<Role> getAll(Role role) {
		return roleMapper.findAll(role);
	}

	public int saveRightList(String ids,String roleId) {
		int i=0;
		//先删除roleId的所有权限，再添加
		int k=roleResourceMapper.remove(roleId);
		if(ids.length()>0){
			String[] idArr=ids.split(",");
			for (int n=0;n<idArr.length;n++){
				String resourceId=idArr[n];
				i+=roleResourceMapper.insert(new RoleResource(UUIDGenerator.getUUID(),roleId,resourceId));
			}
		}
		return i;
	}

}
